<?php

require_once dirname(__FILE__).'/../lib/denominationGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/denominationGeneratorHelper.class.php';

/**
 * denomination actions.
 *
 * @package    maslogger
 * @subpackage denomination
 * @author     Michaël VEROUX
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class denominationActions extends autoDenominationActions
{
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));
	try{
		$versions = Doctrine::getTable('MlMachines')->getAllVersions();
		foreach($versions AS $version){
			if(in_array($this->getRoute()->getObject()->getId(),$version->get('denomination'))){
				throw new Exception('Constraint key');
			}
		}
	    if ($this->getRoute()->getObject()->delete())
	    {
	      $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
	    }
	}
	catch(Exception $e){
    	$this->getUser()->setFlash('error', 'Cette dénomination est utilisée ! Impossible de l\'effacer.');
    }
    $this->redirect('@ml_denominations');
  }	
  protected function executeBatchDelete(sfWebRequest $request)
  {
    $ids = $request->getParameter('ids');

    $records = Doctrine_Query::create()
      ->from('mlDenominations')
      ->whereIn('id', $ids)
      ->execute();
	$versions = Doctrine::getTable('MlMachines')->getAllVersions();
	try{	
    foreach ($records as $record)
    {
		foreach($versions AS $version){
			if(in_array($record->getId(),$version->get('denomination'))){
				throw new Exception('Constraint key');
			}
		}    	
      $record->delete();
    $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
    }
	}
	catch(Exception $e){
    	$this->getUser()->setFlash('error', 'Il y a des dénominations utilisées ! Impossible d\'effacer.');
    }

    $this->redirect('@ml_denominations');
  }  
}
